//---------------------------------------------------------------------
// Authors: jachymko
//
// Description: Class to implement the Set-Clipboard cmdlet.
//
// Creation Date: Dec 12, 2006
// 
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.ComponentModel;
using System.IO;
using System.Management.Automation;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using WinFormsClipboard = System.Windows.Forms.Clipboard;

namespace Pscx.Commands.Clipboard
{
    [Cmdlet(VerbsCommon.Set, PscxNouns.Clipboard, DefaultParameterSetName = ParamSetText)]
    [Description("Copies the item in the system clipboard.")]
    [RelatedLinks("Get-Clipboard"), RelatedLinks("Out-Clipboard"), RelatedLinks("Write-Clipboard")]
    public class SetClipboardCommand : ClipboardCommandBase
    {
        private Image _image;
        private StringCollection _paths = new StringCollection();
        private FileSystemInfo[] _files = new FileSystemInfo[0];
        private string _text = string.Empty;
        private string _html = string.Empty;
        private string _rtf = string.Empty;
        
        const string ParamSetRtf = "Rtf";
        const string ParamSetHtml = "Html";
        const string ParamSetText = "Text";
        const string ParamSetFiles = "Files";
        const string ParamSetImage = "Image";

        [AllowNull]
        [Parameter(ValueFromPipeline = true, ParameterSetName = ParamSetImage)]
        public Image Image
        {
            get { return _image; }
            set { _image = value; }
        }

        [AllowNull]
        [AllowEmptyCollection]
        [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true, ParameterSetName = ParamSetFiles)]
        public FileSystemInfo[] Files
        {
            get { return _files; }
            set { _files = value; }
        }

        [AllowNull]
        [AllowEmptyString]
        [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true, ParameterSetName = ParamSetText)]
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true, ParameterSetName = ParamSetHtml)]
        public string Html
        {
            get { return _html; }
            set { _html = value; }
        }

        [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true, ParameterSetName = ParamSetRtf)]
        public string Rtf
        {
            get { return _rtf; }
            set { _rtf = value; }
        }

        protected override void ProcessRecord()
        {
            if (_files != null)
            {
                foreach (FileSystemInfo fsi in _files)
                {
                    if (fsi.Exists) _paths.Add(fsi.FullName);
                }
            }

            base.ProcessRecord();
        }

        protected override void EndProcessing()
        {
            ExecuteWrite(delegate
            {
                switch (ParameterSetName)
                {
                    case ParamSetFiles:
                        if (_paths.Count == 0)
                            WinFormsClipboard.Clear();
                        else
                            WinFormsClipboard.SetFileDropList(_paths);
                        break;

                    case ParamSetImage:
                        if (_image == null)
                            WinFormsClipboard.Clear();
                        else
                            WinFormsClipboard.SetImage(_image);
                        break;

                    case ParamSetRtf:
                        SetTextContents(_rtf, TextDataFormat.Rtf);
                        break;

                    case ParamSetHtml:
                        SetTextContents(_html, TextDataFormat.Html);
                        break;

                    default:
                        SetTextContents(_text, TextDataFormat.UnicodeText);
                        break;
                }
            });
        }

        void SetTextContents(string value, TextDataFormat format)
        {
            if (string.IsNullOrEmpty(value))
            {
                WinFormsClipboard.Clear();
            }
            else
            {
                WinFormsClipboard.SetText(value, format);
            }
        }
    }
}


